﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Marmat.Template.WinForm
{
    /// <summary>
    /// 事件总线
    /// </summary>
    public static class EventBus
    {
        /// <summary>
        /// 发布事件。
        /// </summary>
        ///<param name="channel">通道名称</param>
        ///<param name="body">事件内容</param>
        public static void Publish<TEvent>(string channel, TEvent body) where TEvent : IEventBase
        {
            IEventBus eventBus = Marmat.FrameWork.SystemCommand.GetService<IEventBus>();
            eventBus.Publish(channel, body);
        }

        /// <summary>
        /// 订阅事件。
        /// </summary>
        ///<param name="channel">通道名称</param>
        ///<param name="handler">处理事件</param>
        public static void Subscribe<TEvent>(string channel, IEventHandler<TEvent> handler) where TEvent : IEventBase
        {
            IEventBus eventBus = Marmat.FrameWork.SystemCommand.GetService<IEventBus>();
            eventBus.Subscribe(channel, handler);
        }

        /// <summary>
        /// 移除订阅的事件。
        /// </summary>
        /// <param name="handler"></param>
        public static void RemoveSubscription<TEvent>(IEventHandler<TEvent> handler) where TEvent : IEventBase
        {
            IEventBus eventBus = Marmat.FrameWork.SystemCommand.GetService<IEventBus>();
            eventBus.RemoveSubscription(handler);
        }
    }
}
